<template>
  <div id="userLoginPage">
    <h1 style="margin-bottom: 16px">用户登录</h1>
    <a-form
      :model="form"
      style="max-width: 480px; margin: 0 auto"
      auto-label-width
      label-align="left"
      @submit="handleSubmit"
    >
      <a-form-item
        field="userAccount"
        label="账号"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        field="userPassword"
        label="密码"
        :rules="[{ required: true, message: '请输入密码' }]"
        tooltip="密码不少于8位"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button html-type="submit" type="primary">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { userLogin } from "@/api/user";
import { useLoginUserStore } from "@/store/useLoginUserStore";
import { useRouter } from "vue-router";
import message from "@arco-design/web-vue/es/message";

const loginUserStore = useLoginUserStore();

const router = useRouter();

interface UserLoginRequest {
  userAccount: string;
  userPassword: string;
}

const form = ref<UserLoginRequest>({
  userAccount: "",
  userPassword: "",
});

const handleSubmit = async () => {
  const res = await userLogin(form.value);
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser();
    message.success("登录成功");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败");
  }
};
</script>
<style scoped>
#userLoginPage {
  text-align: center;
}
</style>
